package parameters;

import java.nio.ByteBuffer;

public class Vec2f {
	public float x,y;
	
	public Vec2f(){
		this(0,0);
	}

	public Vec2f(float x, float y){
		this.x = x;
		this.y = y;
	}
	
	public Vec2f(Vec2f vec){
		this(vec.x, vec.y);
	}
	
	public static float getDistance(Vec2f vec, Vec2f vec2){
		float tmpX = Math.abs(vec2.x - vec.x);
		float tmpY = Math.abs(vec2.y - vec.y);
		
		return (float)( Math.sqrt(tmpX * tmpX + tmpY * tmpY));
	}
	
	public static byte[] toBytes(Vec2f vec){
		ByteBuffer bb = ByteBuffer.allocate(8);
		
		bb.putFloat(vec.x);
		bb.putFloat(vec.y);
		
		bb.flip();
		
		return bb.array();
	}
	
	public static Vec2f fromBytes(byte[] b){  
	    ByteBuffer buf = ByteBuffer.wrap(b);  
	    
	    float x = buf.getFloat(0);
	    float y = buf.getFloat(4);
	    
	    Vec2f tmp = new Vec2f(x, y);
	    
	    return tmp;
	}
}
